package com.ghsc.ghxc.jsb.ui.integral.activity

import android.view.View
import androidx.fragment.app.Fragment
import androidx.viewpager.widget.ViewPager
import com.cn.appcore.adapter.PagerAdapter
import com.cn.appcore.base.BaseVMActivity
import com.cn.appcore.event.RxBus
import com.cn.appcore.event.RxBusBean
import com.flyco.tablayout.listener.OnTabSelectListener
import com.ghsc.ghxc.jsb.R
import com.ghsc.ghxc.jsb.config.CustKey
import com.ghsc.ghxc.jsb.databinding.ActivityTabBinding
import com.ghsc.ghxc.jsb.ext.intentExtras
import com.ghsc.ghxc.jsb.ui.integral.IntegralSupermarketViewModel
import com.ghsc.ghxc.jsb.ui.integral.fragment.MyBuyListFragment
import com.ghsc.ghxc.jsb.ui.integral.fragment.MyReleaseListFragment
import com.ghsc.ghxc.jsb.ui.integral.fragment.MySellListFragment
import java.util.ArrayList

/**
 * 项目：GuoHuaJsb
 * 包名：com.ghsc.ghxc.jsb.ui.integral.activity
 * 类名：TabActivity
 * 创建时间：2024/5/20 11:58
 * 创建人 @author： xiaolin
 * 描述 ：Todo...
 */
class TabActivity : BaseVMActivity<IntegralSupermarketViewModel, ActivityTabBinding>() ,
    OnTabSelectListener {
    override fun attachLayoutRes(): Int {
        return R.layout.activity_tab
    }

    override fun attachVMClass(): Class<IntegralSupermarketViewModel>? {
        return IntegralSupermarketViewModel::class.java
    }

    override fun initBar() {
        super.initBar()
        mImmersionBar!!.statusBarDarkFont(true).init()
    }

    private val type: Int by intentExtras("type",0)

    var mList: MutableList<String> = ArrayList()
    var mAdapter: PagerAdapter? = null
    private val mFragments: MutableList<Fragment> = ArrayList()
    override fun initView() {
        binding.apply {
            bar.topTitle.text = if(type==1) "我发布的" else if(type==2) "我卖出的" else "我买到的"
            bar.topLeftView.setOnClickListener { finish() }
            bar.topRightView.visibility = View.GONE
        }
        mList.clear()
        mFragments.clear()
        if(type==1){
            mList.add("通过审核")
            mList.add("待审核")
            mList.add("审核未通过")
            MyReleaseListFragment.getInstance(1)?.let { mFragments.add(it) }
            MyReleaseListFragment.getInstance(0)?.let { mFragments.add(it) }
            MyReleaseListFragment.getInstance(3)?.let { mFragments.add(it) }
        }else if(type==2){
            mList.add("待发货")
            mList.add("待收货")
            mList.add("已收货")
            mList.add("退换货")
            MySellListFragment.getInstance(1)?.let { mFragments.add(it) }
            MySellListFragment.getInstance(3)?.let { mFragments.add(it) }
            MySellListFragment.getInstance(5)?.let { mFragments.add(it) }
            MySellListFragment.getInstance(4)?.let { mFragments.add(it) }
        }else if(type==3){
            mList.add("待发货")
            mList.add("待收货")
            mList.add("已收货")
            mList.add("退换货")
            MyBuyListFragment.getInstance(1)?.let { mFragments.add(it) }
            MyBuyListFragment.getInstance(3)?.let { mFragments.add(it) }
            MyBuyListFragment.getInstance(5)?.let { mFragments.add(it) }
            MyBuyListFragment.getInstance(4)?.let { mFragments.add(it) }
        }
        mAdapter = PagerAdapter(
            supportFragmentManager,
            mList,
            mFragments
        )
        binding.vpOrder.adapter = mAdapter
        binding.vpOrder.offscreenPageLimit = mList.size - 1
        binding.stlTab.setViewPager(binding.vpOrder)
        binding.stlTab.setOnTabSelectListener(this@TabActivity)
        binding.stlTab.currentTab = 0
        binding.vpOrder.addOnPageChangeListener(object : ViewPager.OnPageChangeListener {
            override fun onPageScrolled(
                position: Int,
                positionOffset: Float,
                positionOffsetPixels: Int
            ) {
            }

            override fun onPageSelected(position: Int) {
                tabIndex(position)
            }

            override fun onPageScrollStateChanged(state: Int) {}
        })
    }
    override fun onTabSelect(position: Int) {
        tabIndex(position)
    }

    override fun onTabReselect(position: Int) {
    }
    private fun tabIndex(index: Int) {
        var orderIndex =0
        if(type==1){
            if (index ==0) orderIndex=1
            else if (index ==1) orderIndex=0
            else if (index ==2) orderIndex=3
        }
        if(type==3 || type ==2 ){
            if (index ==0) orderIndex=1
            else if (index ==1) orderIndex=3
            else if (index ==2) orderIndex=5
            else if (index ==3) orderIndex=4
        }
        RxBus.getDefault().post(RxBusBean(CustKey.ORDER_TAB, CustKey.ORDER_TAB,orderIndex))
    }
}